package com.atguigu.gulimall.search.controller;

import com.atguigu.gulimall.search.service.SearchService;
import com.atguigu.gulimall.search.vo.SearchParam;
import com.atguigu.gulimall.search.vo.SearchResult;
import com.atguigu.gulimall.search.vo.SearchReult;
import com.atguigu.gulimall.search.vo.v1.SearchByWeightParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author guoch0613@163.com
 * @Description
 * @create 2021-04-03 14:25
 */
@Api(tags = "mall-search-controller", value = "/api",description = "搜索-仓储-列表-控制器")
@Controller
@Slf4j
public class SearchController {

    @Autowired
    SearchService searchService;

    @ApiOperation("引擎")
    @GetMapping(value = {"/list.html","/"})
    public String getSearchPage(SearchParam searchParam, Model model, HttpServletRequest request) {
        searchParam.set_queryString(request.getQueryString());
        log.debug("searchParam,{}",searchParam);
        SearchResult result= searchService.getSearchResult(searchParam);
        model.addAttribute("result", result);
        return "list";
    }

    @ResponseBody
    @PostMapping(value = "/hello")
    public SearchReult getWeight(@RequestBody SearchByWeightParam searchParam) {
        log.debug("searchParam,{}",searchParam);
        SearchReult result= searchService.searchSortByWeight(searchParam);
        return result;
    }
}
